 
const  moment =  require('moment');
const { Controller } = require('egg');

class BaseController extends Controller {  
  
	args(){
        const { ctx } = this;
        return Object.assign(ctx.query,ctx.params,ctx.request.body,ctx.header,ctx.originalUrl)
	} 
	success(data) {
		this.ctx.body = { code:1, success: true, data, };
	}
	error(msg) {
		this.ctx.body = { code:0, success: false, msg, };
	}
    check(){
        console.log(this.ctx.state.user); // { username: '008615019469294', iat: 1625657627 } // 过期时间
        var userTime = this.ctx.state.user.iat * 1000; 
        var token = this.ctx.session.token   // session is {} after logout   
        console.log(userTime,Date.now(),token,)
        if(!token  ) {
            console.log("登陆失效")
            this.error("登陆失效")  
            return  true  
        }
        // 60 * 60 *　1000 * 6 // 6 小时失去效果
        if(userTime + 1000 * 60 < Date.now() ) { // userTime < Date.now()
            console.log("登陆失效")
            this.error("登陆失效")  
            return  true  
        }
    }
    formatDate(date=Date.now(),format="YYYY-MM-DD HH:mm:ss") {
		return moment(date).format(format);
	}
}
module.exports = BaseController;